package com.layer8.RPS;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;

/**
 * User: agent_000
 * Date: 12/31/12
 * Time: 1:02 PM
 */

public class PetPicker extends Activity {

    GridView gv;
    int petID;
    final static int PET_PICKER = 100;

    private static ArrayList<Pet> pets = new ArrayList<Pet>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.petpick);
        loadPets();
        getSavedPet();
        gv = (GridView) findViewById(R.id.petGrid);
        gv.setAdapter(new PetListAdapter(this));
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle data = new Bundle();
                data.putInt("petID", i);
                Intent intent = new Intent(getApplicationContext(), PetChooser.class);
                intent.putExtras(data);
                startActivityForResult(intent, PET_PICKER);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PET_PICKER) {
            if (resultCode == RESULT_OK) {
                petID = data.getIntExtra("petID", 0);
                gv.invalidateViews();
            }
        }
    }

    private void loadPets() {
        PetLoader petLoader;
        petLoader = new PetLoader(this);
        pets = petLoader.loadPetList();
    }

    private void getSavedPet() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        petID = prefs.getInt("petID", 0);
    }

    public class PetListAdapter extends BaseAdapter {

        Context mContext;

        public PetListAdapter(Context mContext) {
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            return pets.size();
        }

        @Override
        public Object getItem(int i) {
            return pets.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View grid;
            if (view == null) {
                LayoutInflater inflater = getLayoutInflater();
                grid = inflater.inflate(R.layout.petlayout, null);
            } else {
                grid = view;
            }
            LinearLayout background = (LinearLayout) grid.findViewById(R.id.llGridLayout);
            Drawable d;
            if (i == petID) {
                d = getResources().getDrawable(R.drawable.selectedpetback);
            } else {
                d = getResources().getDrawable(R.drawable.petback);
            }
            background.setBackgroundDrawable(d);
            TextView name = (TextView) grid.findViewById(R.id.txtPetName);
            Pet pet = pets.get(i);
            name.setText(pet.getName());
            d = getResources().getDrawable(pet.getImage());
            name.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
            return grid;
        }
    }

}
